﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Xin478.CSWork.Common;

namespace Xin478.CSWork.Client
{
    public partial class TaskForm : Form
    {
        private static object lockHelper=new object();
        private static TaskForm instance;
        private Dictionary<int, TabItem> openTabs = new Dictionary<int, TabItem>();

        public static TaskForm Instance
        {
            get
            {
                lock (lockHelper)
                {
                    if (instance == null) instance = new TaskForm();
                }
                return instance;
            }
        }

        protected TaskForm()
        {
            InitializeComponent();
            this.Icon = MainFormResource.Conntected;
            this.tabCtrl.TabItemClose+=new TabStrip.UserActionEventHandler(tabCtrl_TabItemClose);
            this.Closing += new CancelEventHandler(TaskForm_Closing);
        }

        void TaskForm_Closing(object sender, CancelEventArgs e)
        {
            if(this.tabCtrl.Tabs.Count==1 || MessageBox.Show("你确定要关闭所有标签吗?","",MessageBoxButtons.YesNo)==DialogResult.Yes)
            {
                CloseAll();
            }
            e.Cancel = true;
        }

        void tabCtrl_TabItemClose(object sender, TabStripActionEventArgs e)
        {
            if (this.tabCtrl.Tabs.Count == 1) CloseAll();
            else
            {
                WorkInfo workInfo = this.tabCtrl.SelectedTab.Tag as WorkInfo;
                lock (lockHelper)
                {
                    openTabs.Remove(workInfo.Id);
                    this.tabCtrl.Tabs.Remove(this.tabCtrl.SelectedTab);
                }
            }
            e.Cancel = true;
        }

        void CloseAll()
        {
            lock (lockHelper)
            {
                openTabs.Clear();
                this.tabCtrl.Tabs.Clear();
                this.tabCtrl.Dispose();
                instance = null;
                this.Dispose();
            }
        }

        public void ShowTask(WorkInfo workInfo, Form form)
        {
            if (openTabs.ContainsKey(workInfo.Id))
            {
                tabCtrl.SelectedTab = openTabs[workInfo.Id];
            }
            else
            {
                TabItem tabItem= tabCtrl.CreateTab(workInfo.Title);
                openTabs[workInfo.Id] = tabItem;
                tabItem.Tag = workInfo;
                tabCtrl.SelectedTab = tabItem;
                /*
                TaskInfoCtrl taskInfo = new TaskInfoCtrl(workInfo);
                taskInfo.Dock = DockStyle.Fill;
                TabControlPanel tp = new TabControlPanel();
                tp.Dock = System.Windows.Forms.DockStyle.Fill;
                tp.TabItem = tabItem;
                tp.Controls.Add(taskInfo);
                
                this.tabCtrl.Controls.Add(tp);
                tabItem.AttachedControl = tp;*/
            }

            if (this.WindowState == FormWindowState.Minimized && this.Visible == false)
            {
                this.Show();
                return;
            }
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;

            if (this.Visible == false) this.Show(form);
        }

    }
}
